package com.sdry.product.prod.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdry.product.prod.entity.ProductWarehousing;
import com.sdry.product.prod.mapper.ProductWarehousingMapper;
import com.sdry.system.entity.SysUser;
import com.sdry.system.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (ProductWarehousing)表服务实现类
 *
 * @author makejava
 * @since 2020-03-03 16:21:03
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ProductWarehousingServiceImpl implements ProductWarehousingService {
    @Resource
    private ProductWarehousingMapper productWarehousingDao;

    @Resource
    private UserMapper userMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param proId 主键
     * @return 实例对象
     */
    @Override
    public ProductWarehousing queryById(Long proId) {
        return this.productWarehousingDao.queryById(proId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<ProductWarehousing> queryAllByLimit(int offset, int limit) {
        return this.productWarehousingDao.queryAllByLimit(offset, limit);
    }

    @Override
    public Long queryAllByTotal(ProductWarehousing productWarehousing) {
        QueryWrapper<ProductWarehousing> queryWrapper = new QueryWrapper<>();
        return Long.valueOf(productWarehousingDao.selectCount(queryWrapper));
    }

    /**
     * 查询多条数据
     * @param productWarehousing 对象列表
     * @return
     */
    @Override
    public List<ProductWarehousing> queryAllByLimit(ProductWarehousing productWarehousing) {
        QueryWrapper<ProductWarehousing> queryWrapper = new QueryWrapper<>();
        Page<ProductWarehousing> page = new Page<>(productWarehousing.getPage(),productWarehousing.getLimit());
        IPage<ProductWarehousing> productWarehousingIPage = productWarehousingDao.selectPage(page, queryWrapper);
        List<ProductWarehousing> warehousingList = productWarehousingIPage.getRecords();
        for (ProductWarehousing warehousing : warehousingList) {
            SysUser user = userMapper.selectById(warehousing.getCreateBy());
            warehousing.setRealName(user.getRealName());
        }

        return warehousingList;
    }

    /**
     * 新增数据
     *
     * @param productWarehousing 实例对象
     * @return 实例对象
     */
    @Override
    public ProductWarehousing insert(ProductWarehousing productWarehousing) {
        this.productWarehousingDao.insert(productWarehousing);
        return productWarehousing;
    }

    /**
     * 修改数据
     *
     * @param productWarehousing 实例对象
     * @return 实例对象
     */
    @Override
    public ProductWarehousing update(ProductWarehousing productWarehousing) {
        this.productWarehousingDao.update(productWarehousing);
        return this.queryById(productWarehousing.getProId());
    }

    /**
     * 通过主键删除数据
     *
     * @param proId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long proId) {
        return this.productWarehousingDao.deleteById(proId) > 0;
    }
}